package com.example.javatwo.service.cg.yt;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.example.javatwo.mapper.cg.lmx.WuLiaoMapper;
import com.example.javatwo.mapper.cg.wmj.buyReturnMapping;
import com.example.javatwo.mapper.cg.yt.CgftMapper;
import com.example.javatwo.pojo.cg.lmx.Product;
import com.example.javatwo.pojo.cg.yt.DsAssignBuy;
import com.example.javatwo.pojo.cg.yt.DsAssignBuyMain;
import com.example.javatwo.util.PageBead;

@Service
public class CgftService {
	@Autowired
	private CgftMapper cgftMapper;
	
	@Autowired
	private WuLiaoMapper wuLiaoMapper;
	
	@Autowired
	private buyReturnMapping ser;
	
	public PageBead list(int sum){
		List<DsAssignBuyMain> cg = cgftMapper.list(sum-1);
		for (DsAssignBuyMain dsAssignBuyMain : cg) {
			dsAssignBuyMain.setDsAssignBuys(cgftMapper.list2(dsAssignBuyMain.getBillNo()));
			dsAssignBuyMain.setDsAssignFees(cgftMapper.list3(dsAssignBuyMain.getBillNo()));
		}
		PageBead page =new PageBead(1, cgftMapper.sum(), sum, 1, cg);
		System.out.println(page);
		return page;
	}
	
	@Transactional
	public void add(DsAssignBuyMain dssassignBuyMain){
		cgftMapper.add(dssassignBuyMain);
		System.out.println("近");
		cgftMapper.addInfo(dssassignBuyMain);
	}
	
	@Transactional
	public void update(DsAssignBuyMain dssassignBuyMain){
		cgftMapper.update(dssassignBuyMain);
	}
	
	@Transactional
	public void del(String billNo){
		cgftMapper.delete(billNo);
	}
	 
	public String auditing(DsAssignBuyMain dssassignBuyMain){
		System.out.println(dssassignBuyMain);
		if(cgftMapper.findSet1(dssassignBuyMain.getBillNo()).equals("是")){
			System.out.println("近");
			return null;
		}
		for (DsAssignBuy item : dssassignBuyMain.getDsAssignBuys()) {
			Product wuliao= wuLiaoMapper.selectId(item.getProdID());
			System.out.println(wuliao);
			double callcost =0-wuliao.getCallCost();
			int callamt=wuliao.getCallAmt();
			System.out.println(callcost+"=="+callamt+"==");
			ser.UpdateProduct(0, callcost, item.getProdID());
		}
		cgftMapper.setYnAudit(dssassignBuyMain.getBillNo(),"是");
		return "审核成功";
	}
	public String auditing1(DsAssignBuyMain dssassignBuyMain){
		if(cgftMapper.findSet1(dssassignBuyMain.getBillNo()).equals("否")){
			System.out.println("近");
			return null;
		}
		for (DsAssignBuy item : dssassignBuyMain.getDsAssignBuys()) {
			Product wuliao= wuLiaoMapper.selectId(item.getProdID());
			System.out.println(wuliao);
			double callcost =item.getMlAmount();
			int callamt=wuliao.getCallAmt();
			ser.UpdateProduct(0, callcost, item.getProdID());
			System.out.println(callcost+"=="+callamt+"==");
		}
		cgftMapper.setYnAudit(dssassignBuyMain.getBillNo(),"否");
		
		return "反審核成功";
	}
}
